﻿namespace Okazuki.MVVM.PrismSupport.FSharp.Test
{
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Practices.Prism.Commands;
    using System.Windows.Input;

    [TestClass]
    public class FsViewModelBaseTest
    {
        class TestViewModel : FsViewModelBase
        {
            public bool SampleExecuted { get; set; }
            public bool CanSampleExecuted { get; set; }

            private string name;
            public string Name
            {
                get
                {
                    return name;
                }

                set
                {
                    base.set(ref name, value, "Name");
                }
            }

            private DelegateCommand sampleCommand;
            public DelegateCommand SampleCommand
            {
                get
                {
                    return base.command(ref sampleCommand,
                        () => this.SampleExecuted = true,
                        () => this.CanSampleExecuted = true);
                }
            }

        }

        private TestViewModel target;

        [TestInitialize]
        public void SetUp()
        {
            this.target = new TestViewModel();
        }

        [TestCleanup]
        public void TearDown()
        {
            this.target = null;
        }

        [TestMethod]
        public void TestPropertyChangedCalled()
        {
            var called = false;
            this.target.PropertyChanged += (s, e) => called = true;
            Assert.IsFalse(called);
            this.target.Name = "Tanaka";
            Assert.IsTrue(called);
            Assert.AreEqual("Tanaka", this.target.Name);
        }

        [TestMethod]
        public void TestCommandExecute()
        {
            Assert.IsFalse(this.target.SampleExecuted);
            Assert.IsFalse(this.target.CanSampleExecuted);

            this.target.SampleCommand.Execute();
            Assert.IsTrue(this.target.SampleExecuted);

            this.target.SampleCommand.CanExecute();
            Assert.IsTrue(this.target.CanSampleExecuted);
        }
    }
}
